//
//  DIYCalendarCellForScheduleDateOfInstall.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/4/3.
//

import UIKit
import FSCalendar
class DIYCalendarCellForScheduleDateOfInstall: FSCalendarCell {
    weak var selectBackLayer: CAShapeLayer!//自定义选中的背景颜色
    weak var todayBackLayer: CAShapeLayer!//自定义今天的黑色圆角背景，选中时在叠加上selectBackLayer
    weak var eventCircleLayer: CAShapeLayer!//自定义event的circlelayer
    required init!(coder aDecoder: NSCoder!) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        //selectBackLayer
        let selectBackLayer = CAShapeLayer()
        selectBackLayer.fillColor = UIColor(hexStr: "#9FACBE80").cgColor
        selectBackLayer.actions = ["hidden": NSNull()]
        self.contentView.layer.insertSublayer(selectBackLayer, below: self.titleLabel!.layer)
        self.selectBackLayer = selectBackLayer
        self.selectBackLayer.isHidden = true
        //todayBackLayer
        let todayBackLayer = CAShapeLayer()
        todayBackLayer.fillColor = UIColor.black.cgColor
        todayBackLayer.actions = ["hidden": NSNull()]
        self.contentView.layer.insertSublayer(todayBackLayer, below: self.titleLabel!.layer)
        self.todayBackLayer = todayBackLayer
        self.todayBackLayer.isHidden = true
        //eventLayer
        let eventCircleLayer = CAShapeLayer()
//        eventCircleLayer.fillColor = UIColor.blue.cgColor
        eventCircleLayer.actions = ["hidden": NSNull()]
        self.contentView.layer.insertSublayer(eventCircleLayer, below: self.titleLabel!.layer)
        self.eventCircleLayer = eventCircleLayer
        self.eventCircleLayer.isHidden = true
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        self.titleLabel.frame = self.contentView.frame
        self.shapeLayer.isHidden = true//隐藏默认的选中背景layer，使用selectBackLayer
        //selectBackLayer
        let contentBounds = self.contentView.bounds
        let selectBackSize: CGFloat = contentBounds.size.height
        let selectBackFrame = CGRect(x: contentBounds.origin.x + (contentBounds.size.width - selectBackSize) / 2, y: contentBounds.origin.y + (contentBounds.size.height - selectBackSize) / 2, width: selectBackSize, height: selectBackSize)
        self.selectBackLayer.frame = selectBackFrame
        self.selectBackLayer.path = UIBezierPath(rect: self.selectBackLayer.bounds).cgPath
        //todayBackFrame
        let todayBackSize: CGFloat = 25
        let todayBackFrame = CGRect(x: selectBackFrame.origin.x + (selectBackFrame.size.width - todayBackSize) / 2, y: selectBackFrame.origin.y + (selectBackFrame.size.height - todayBackSize) / 2, width: todayBackSize, height: todayBackSize)
        self.todayBackLayer.frame = todayBackFrame
        self.todayBackLayer.path = UIBezierPath(roundedRect: self.todayBackLayer.bounds, cornerRadius: 6).cgPath
        //todayBackFrame
        let circleSize: CGFloat = 8
        let circleFrame = CGRect(x: todayBackFrame.origin.x + (todayBackFrame.size.width - circleSize) / 2, y: todayBackFrame.origin.y + todayBackSize + 2, width: circleSize, height: circleSize)
        self.eventCircleLayer.frame = circleFrame
        self.eventCircleLayer.path = UIBezierPath(roundedRect: self.eventCircleLayer.bounds, cornerRadius: circleSize / 2).cgPath
    }
    override func configureAppearance() {
        super.configureAppearance()
    }
}
